package com.own.business.sync.trigger.base.util;

import com.own.business.sync.trigger.base.config.SyncTriggerCondition;
import com.own.business.sync.trigger.base.config.SyncTriggerMethod;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.mapper.SyncTriggerMapper;
import com.own.business.sync.trigger.base.property.SyncTriggerProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * SyncTriggerUtil
 *
 * @author chenxueli
 * @date 2024-01-08 16:03:00
 */
@Slf4j
@Component
@Conditional(SyncTriggerCondition.class)
public class SyncTriggerUtil implements SyncTriggerMethod {

    @Resource
    private SyncTriggerProperty syncTriggerProperty;

    @Resource
    private SyncTriggerMapper syncTriggerMapper;

    /**
     * 保存信息
     *
     * @param targetId   源数据id
     * @param type       操作类型 1=插入 2=修改 3=删除
     * @param moduleName 操作模块
     * @param oldStatus  旧状态
     * @param newStatus  新状态
     */
    @Override
    public void add(Long targetId, int type, String moduleName, Integer oldStatus, Integer newStatus) {
        var entity = new SyncTrigger();
        entity.setTargetId(targetId);
        entity.setType(type);
        entity.setModuleName(moduleName);
        entity.setOldStatus(oldStatus);
        entity.setNewStatus(newStatus);
        entity.setAppKey(syncTriggerProperty.getAppKey());
        entity.setGmtCreate(LocalDateTime.now());
        syncTriggerMapper.insert(entity);
    }

}
